package com.kaiyiweitong.tongxing.activity.my.setting;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.kaiyiweitong.tongxing.R;
import com.kaiyiweitong.tongxing.base.BaseActivity;
import com.kaiyiweitong.tongxing.util.CommonUtil;
import com.kaiyiweitong.tongxing.util.MyTextUtils;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/12/19 0019.
 */

public class ChangePswActivity extends BaseActivity {
    @Bind(R.id.title)
    TextView mTitle;
    @Bind(R.id.input_original_psw)
    EditText mInputOriginalPsw;
    @Bind(R.id.original_psw_clear)
    ImageView mOriginalPswClear;
    @Bind(R.id.input_new_psw)
    EditText mInputNewPsw;
    @Bind(R.id.new_psw_clear)
    ImageView mNewPswClear;
    @Bind(R.id.confirm_psw)
    EditText mConfirmPsw;
    @Bind(R.id.psw_clear)
    ImageView mPswClear;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_psw);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        mTitle.setText(getResources().getString(R.string.change_psw));
        addListener(mInputOriginalPsw, mOriginalPswClear);
        addListener(mInputNewPsw, mNewPswClear);
        addListener(mConfirmPsw, mPswClear);
    }

    @OnClick({R.id.original_psw_clear, R.id.new_psw_clear, R.id.psw_clear, R.id.finish})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.original_psw_clear:
                mInputOriginalPsw.setText("");
                break;
            case R.id.new_psw_clear:
                mInputNewPsw.setText("");
                break;
            case R.id.psw_clear:
                mConfirmPsw.setText("");
                break;
            case R.id.finish:
                if (MyTextUtils.checkTextContent(mInputOriginalPsw).equals("")) {
                    CommonUtil.toast(this, R.string.old_psw_not_blank);
                } else if (MyTextUtils.checkTextContent(mInputNewPsw).equals("")) {
                    CommonUtil.toast(this, R.string.new_psw_not_blank);
                } else if (MyTextUtils.checkTextContent(mConfirmPsw).equals("")) {
                    CommonUtil.toast(this, R.string.second_psw_not_blank);
                } else if (!MyTextUtils.checkTextContent(mInputNewPsw).equals(MyTextUtils.checkTextContent(mConfirmPsw))) {
                    CommonUtil.toast(this, R.string.double_psw_not_same);
                } else {

                }
                break;
        }
    }

    private void addListener(final EditText editText, final ImageView ivClear) {
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (MyTextUtils.checkTextContent(editText).equals("")) {
                    ivClear.setVisibility(View.INVISIBLE);
                } else {
                    ivClear.setVisibility(View.VISIBLE);
                }
            }
        });
    }
}
